<?php

namespace app\api\service;

use think\facade\Db;

class UserAddressService
{

    
    /**
     * 删除标签
     */
    public static function  delLabel($data)
    {
        $rst = Db::name('user_address_label')->where(['id'=>$data['id'], 'store_id'=>$data['store_id']
        ,'uid'=>$data['uid']
        ])->update(['delete_time'=>time()]);
        return $rst;
    }

    /**
     * 添加标签
     */
    public static function addLabel($data)
    {
        $rst = Db::name('user_address_label')
            ->insert(['name' => $data['name'], 'uid' => $data['uid'],
                'store_id' => $data['store_id'], 'create_time' => time()]);
        return $rst;
    }

    public static function getUserAddressList($user_id)
    {
        $addresses = Db::name('user_address')->field('id,receiver_name,label,location_address,receiver_phone,province,city,county,address,is_default')
            ->where(['user_id' => $user_id, 'delete_time' => 0])
            ->select();

        return $addresses;
    }

    public static function findUserAddress($address_id)
    {
        $address = Db::name('user_address')->field('id,receiver_name,lat,lng,location_address,label,receiver_phone,province,city,county,address,is_default')
            ->where('id', $address_id)
            ->find();

        return $address;
    }

    public static function saveUserAddress($data, $label, $user_id)
    {
        if ($data['is_default'] ==1) {
            Db::name('user_address')->where('user_id', $user_id)->update(['is_default' => 0]);
        }
        $res = Db::name('user_address')->insert([
            'user_id' => $data['user_id'],
            'receiver_name' => $data['receiver_name'],
            'receiver_phone' => $data['receiver_phone'],
            'province' => $data['province'],
            'city' => $data['city'],
            'county' => $data['county'],
            'lat' => empty($data['lat']) ? '' : $data['lat'],
            'lng' => empty($data['lng']) ? '' : $data['lng'],
            'location_address' => $data['location_address'],
            'address' => $data['address'],
            'is_default' => $data['is_default'],
            'label' => $label,
            'create_time' => time(),
            'update_time' => time(),
        ]);

        return $res;
    }

    public static function updateUserAddress($data, $label, $user_id)
    {
        $u_data = [
            'receiver_name' => $data['receiver_name'],
            'receiver_phone' => $data['receiver_phone'],
            'province' => $data['province'],
            'city' => $data['city'],
            'label' => $label,
            'county' => $data['county'],
            'location_address' => $data['location_address'],
            'address' => $data['address'],
            'is_default' => $data['is_default'],
            'update_time' => time(),
        ];
        if (!empty($data['lat'])) {
            $u_data['lat'] = $data['lat'];
        }
        if (!empty($data['lng'])) {
            $u_data['lng'] = $data['lng'];
        }
        if ($data['is_default'] ==1) {
            Db::name('user_address')->where('user_id', $user_id)->update(['is_default' => 0]);
        }
        $res = Db::name('user_address')->where('id', $data['address_id'])->update($u_data);

        return $res;
    }

    public static function deleteUserAddress($address_id)
    {
        $res = Db::name('user_address')->where('id', $address_id)->update(['delete_time' => time()]);

        return $res;
    }

}
